package com.igeek.web;

import com.alibaba.druid.support.json.JSONUtils;
import com.google.gson.Gson;
import com.igeek.domain.Pagination;
import com.igeek.domain.Product;
import com.igeek.service.Productservice;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

@WebServlet("/product")
public class ProductServlet extends BaseServlet {
    public void getProductPid(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String pid=request.getParameter("pid");
        saveHistory(request,response,pid);
        Productservice service =new Productservice();
        Product products =service.getProductByPid(pid);
        System.out.println(products);
        request.setAttribute("products",products);
        Gson gson =new Gson();
        String s = gson.toJson(products);
        System.out.println("product:"+s);
        request.getRequestDispatcher("product_info.jsp").forward(request,response);
    }
    private void saveHistory(HttpServletRequest request, HttpServletResponse response, String pid) {
        //把数据保存到cookie
        Cookie[] cookies = request.getCookies();
        boolean isPidsExist = false;
        if(cookies!=null){
            for(Cookie c : cookies){
                //假设保存商品id的cookie key 是pids
                if(c.getName().equals("pids")){
                    isPidsExist = true;//存在pids cookie
                    //1,2,3,4
                    String pids = c.getValue();
                    //总长度为7,去掉重复
                    //把字符串转数组
                    String[] pidsrr = pids.split(",");
                    System.out.println("pidsrr:"+Arrays.toString(pidsrr));
                    //数组转list
                    List<String> list = Arrays.asList(pidsrr);
                    LinkedList<String> pidList = new LinkedList<>(list);
                    //去重
                    if(pidList.contains(pid)){
                        pidList.remove(pid);
                    }
                    //始终把新的pid放在第一个
                    pidList.addFirst(pid);
                    //取前7个
                    String sPid = "";
                    for(int i=0;i<pidList.size();i++){
                        if(i>=7){
                            break;
                        }
                        sPid+=pidList.get(i)+",";
                    }
                    //1,2,3,  去掉最后一个,
                    sPid = sPid.substring(0,sPid.length()-1);
                    System.out.println(sPid);
                    //存入cookie
                    response.addCookie(new Cookie("pids",sPid));
                }
            }
        }
        //判断是否存在pids cookie
        if(!isPidsExist){
            response.addCookie(new Cookie("pids",pid));
        }
    }
    public void getproductcid(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        getHistory(request);
        String cid = request.getParameter("cid");
        String page = request.getParameter("page");
        int nowPage = 1;
        if (page == null) {
            nowPage = 1;
        } else {
            nowPage = Integer.parseInt(page);
        }
        Productservice service = new Productservice();
        Pagination<Product> pagination = service.findProductsByCidPage(nowPage, 12, cid);
        request.setAttribute("pagination", pagination);
        request.setAttribute("cid", cid);
        request.getRequestDispatcher("product_list.jsp").forward(request, response);
    }
    private void getHistory(HttpServletRequest request) {
        Cookie[] cookies = request.getCookies();
        if (cookies != null) {
            for (Cookie c : cookies) {
                if (c.getName().equals("pids")) {
                    String pids = c.getValue();
                    Productservice service = new Productservice();
                    List<Product> products = service.findHistoryProducts(pids);
                    request.setAttribute("products", products);
                }
            }
        }
    }
    public void index(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Productservice service =new Productservice();
        List<Product> hotProduct=service.gethotProducts();
        List<Product> newProduct=service.getnewProducts();
        request.setAttribute("hotProduct",hotProduct);
        request.setAttribute("newProduct",newProduct);
        request.getRequestDispatcher("index.jsp").forward(request,response);
    }
}
